<?php

require_once ('html/htmlHelper.php');
require_once ('utility.php');
require_once 'session/access.php';
require_once 'model/projectModelClass.php';

if (!isAuthorized("populateDropdown.php")) {
    header("location: notAuthorized.php?page=page=populateDropdown");
}

if (isset($_GET['type'])) {
    $type = $_GET['type'];
    //echo $type.'<br>';
    //get the satellites for the given parameter
    if (strcmp($type, "doesFileExist") === 0) {
        updateFinancials();
    } else if (strcmp($type, "updateFinancials") === 0) {
        updateFinancials();
    } else if (strcmp($type, "sat") === 0) {
        $parameterName = $_POST['id'];
        getSatellitesFromParameter($parameterName);
    }
    //user would like to perform sensor maintenance
    else if (strcmp($type, "sensorMaintenance") === 0) {
        $typeMaintenance = $_GET['id'];

        //returns a sensor form
        if (strcmp($typeMaintenance, "New Sensor") === 0) {
            getSensorForm();
        }
        //returns a sensor selector form
        else if (strcmp($typeMaintenance, "Edit Sensor") === 0) {
            getSensorSelector();
        }
    }
    //user would like to perform parameter maintenance
    else if (strcmp($type, "parameterMaintenance") === 0) {
        $typeMaintenance = $_GET['id'];

        //returns an empty parameter form
        if (strcmp($typeMaintenance, "New Parameter") === 0) {
            getParameterForm();
        }
        //return an empty EDR Form 
        else if (strcmp($typeMaintenance, "New EDR") === 0) {
            getEDRForm();
        }
        //returns a sensor selector form
        else if (strcmp($typeMaintenance, "Edit Parameter") === 0) {
            getParameterSelector();
        }
    }
    //user would like to do platfrom maintenance
    else if (strcmp($type, "platformMaintenance") === 0) {
        $typeMaintenance = $_GET['id'];

        //returns a platform selector form
        if (strcmp($typeMaintenance, "Edit Platform") === 0) {
            getPlatformSelector();
        }
        //returns an empty platform agency form
        else if (strcmp($typeMaintenance, "New Platform Agency") === 0) {
            getPlatformAgencyForm();
        }
        //returns an agency selector form
        else if (strcmp($typeMaintenance, "Edit Platform Agency") === 0) {
            getAgencySelector();
        }
        //returns an empty platform programme form
        else if (strcmp($typeMaintenance, "New Platform Programme") === 0) {
            getProgrammeForm();
        }
        //returns a programme selector form
        else if (strcmp($typeMaintenance, "Edit Platform Programme") === 0) {
            getProgrammeSelector();
        } else if (strcmp($typeMaintenance, "New Platform Status") === 0) {
            getNewStatusForm();
        }
    } else if (strcmp($type, "editAgencyForm") === 0) {
        $agency = $_GET['id'];
        $agencyResult = executeSelect("SELECT * FROM spaceagency WHERE spaceAgencyID=$agency");
        getPlatformAgencyForm($agencyResult);
    }
    //returns a populated parameter form
    else if (strcmp($type, "editParameterForm") === 0) {
        if (isset($_GET['id'])) {
            $parameterID = $_GET['id'];
            $paramResult = executeSelect("SELECT * FROM parameter WHERE parameterID=$parameterID");
            getParameterForm($paramResult);
        } else {
            getProgrammeForm();
        }
    }
    //returns a populated programme form
    else if (strcmp($type, "newProgrammeForm") === 0) {
        if (isset($_GET['id'])) {
            $programmeID = $_GET['id'];
            $satResult = executeSelect("SELECT * FROM satelliteprogramme WHERE satelliteProgrammeID=$programmeID");
            getProgrammeForm($satResult);
        } else {
            getProgrammeForm();
        }
    }
    //returns a populated sensor form
    else if (strcmp($type, "editSensorForm") === 0) {
        if (isset($_GET['id'])) {
            $sensorID = $_GET['id'];
            $sensorResult = executeSelect("SELECT * FROM sensor WHERE sensorID=$sensorID");
            getSensorForm($sensorResult);
        } else {
            getSatelliteForm();
        }
    }
    //returns a populated satellite form
    else if (strcmp($type, "newSatForm") === 0) {
        if (isset($_GET['id'])) {
            $satID = $_GET['id'];
            $satResult = executeSelect("SELECT * FROM satellite WHERE satelliteID=$satID");
            getSatelliteForm($satResult);
        } else {
            getSatelliteForm();
        }
    }
    //get the parameters for the given requirement (used when creating a subtask)
    else if (strcmp($type, "parameter") === 0) {
        $reqId = $_GET['id'];
        //old form with parameter, sat, sensor
        //getParametersFromReq($reqId, true, "getSatFromParameter(this.value)");
        getParametersFromReq($reqId, true);
    }
    //get the sensors for the given satellite and EDR
    else if (strcmp($type, "sensor") === 0) {
//echo 'here';
        $satID = $_GET['id'];
        $edrDes = $_POST['edrID'];
//echo 'sat '.$satID.' edr '.$edrDes.'<br>';
        getSensorFromSatAndEdr($satID, $edrDes);
    }
    //get all the sensors this satellite has
    else if (strcmp($type, "sensors") === 0) {
        //echo 'here';
        $satID = $_GET['id'];
        $listType = $_GET['dropdown'];
        //echo 'sat '.$satID.' edr '.$edrDes.'<br>';
        //getSensorsFromSat($satID, $listType);
        getSensorAcronymsOnSat($satID, $listType);
    }

//get the field (list of parameters, source, priority, fy) for given requirement
    else if (strcmp($type, "req") === 0) {
        $reqId = $_GET['id'];
        $field = $_GET['field'];
        getReqData($reqId, $field);
    }

//user wants a list of requirements given the fy
    else if (strcmp($type, "reqList") === 0) {
//used when a user selects an fy and wants a table of requirements returned
        if (isset($_GET['field'])) {
            $fy = $_GET['id'];
            getReqTable($fy);
        } else if (isset($_GET['fy'])) {
            $fy = $_GET['fy'];
            echo getReqList($fy, "getReqFields(this.value)");
        }
//used when a user selects a fy when wanting to edit a requirement
        else {
            $fy = $_GET['fy2'];
            echo getReqList($fy);
        }
    }
} else {
    return "missed it";
}

function doesFileExist() {
    $filename = $_GET['filename'];
    if (file_exists($filename)) {
        return true;
    } else {
        return false;
    }
}

function updateFinancials() {
    $value = $_GET['value'];
    $pizza = $_GET['string'];
    $pieces = explode(",", $pizza); //projectID, taskID, subtaskID, type, Quarter, real (proposed or actual), optional - rebaseline
    $projectID = $pieces[0];
    $taskID = $pieces[1];
    $subtaskID = $pieces[2];
    $type = $pieces[3];
    $quarter = $pieces[4] + 1;
    $real = $pieces[5];
    if (strcmp($real, "proposed") === 0) {
        if (isset($pieces[6]) && strcmp($real, "rebaseline") === 0) {
            $updateResult = executeUpdate("UPDATE subtaskfinancial SET valueProposed=$value WHERE subtaskID=$subtaskID AND type='$type' AND quarter=$quarter");
        }
        else
            $updateResult = executeUpdate("UPDATE subtaskfinancial SET proposedRebaseline=$value WHERE subtaskID=$subtaskID AND type='$type' AND quarter=$quarter");
    }
    else
        $updateResult = executeUpdate("UPDATE subtaskfinancial SET valueActual=$value WHERE subtaskID=$subtaskID AND type='$type' AND quarter=$quarter");
    if ($updateResult) {
        //echo "UPDATE subtaskfinancial SET valueActual=$value WHERE subtaskID=$subtaskID AND type='$type' AND quarter=$quarter";
        projectModelClass::updateProjectCostStatic($projectID, "actualCost");
        echo $value;
    } else {
        echo "error";
    }
}

function getReqTable($fy) {
    $result = executeSelect("SELECT * FROM requirement WHERE fiscalYear=$fy ORDER by priority ASC");
    getRequirementsTable($result, true);
}

function getReqList($fy, $action = NULL) {
    $output = "";
//    if (strcmp($listType, "All") === 0) {
//        getGenericDropdownMenu(getRequirements(), "reqDropdown", "99%", false, "getReqFields(this.value)", "reqDropdown", NULL, "true");
//    } else if (strcmp($listType, "Active") === 0) {
//        getGenericDropdownMenu(getRequirements("Active"), "reqDropdown", "99%", false, "getReqFields(this.value)", "reqDropdown", NULL, "true");
//    } else {
//        getGenericDropdownMenu(getRequirements("Inactive"), "reqDropdown", "99%", false, "getReqFields(this.value)", "reqDropdown", NULL, "true");
//    }
    $output .= '<br>';
    $output .= 'Requirement Name <br>';
    if ($action) {
        $output .= getGenericDropdownMenu(getRequirements($fy), "reqDropdown", "99%", false, $action, "reqDropdown", NULL, "true");
    } else {
        $output .= getReqListBox(getRequirements($fy));
    }
    return $output;
}

function getReqListBox($result) {
    $size = $result->num_rows;
    $output = "";
    $output .= "<select name = \"reqList\" id=\"a\" size=\"$size\" multiple=\"\">";
    $counter = 0;
    while ($row = mysqli_fetch_array($result)) {
        $reqName = $row['description'];
        $output .= "<option value=\"$counter\">$reqName</option>";
    }

//	<option value=\"a\">Afghanistan</option>
//	<option value=\"b\">Bahamas</option>
//	<option value=\"c\">Barbados</option>
//	<option value=\"d\">Belgium</option>
//	<option value=\"e\">Bhutan</option>
//	<option value=\"f\">China</option>
//	<option value=\"g\">Croatia</option>
//	<option value=\"h\">Denmark</option>
//	<option value=\"i\">France</option>
    $output .="
</select>
<br><br>
Move
<input type=\"button\" value = \"Up\" onclick=\"listbox_move('a', 'up')\">
<input type=\"button\" value = \"Down\" onclick=\"listbox_move('a', 'down')\">
</br></br>";

    $output .= "<input type=\"hidden\" id=\"hiddenList\" value=\"hiddenList\" name=\"hiddenList\">";
    $output .= getFormInputElement('submit', 'submit', "100%", "Submit Priority Changes", "ColectSel('a','hiddenList')");
    return $output;
}

/**
 * Used when the user is editing existing requirements
 * @param type $reqId
 * @param type $field
 */
function getReqData($reqId, $field) {

//get the parameters associated with the given requirement
    if (strcmp($field, "parameter") === 0) {
        getParametersFromReq($reqId, false, "getDbFieldsFromParameter(this.value)");
    } else if (strcmp($field, "source") === 0 || strcmp($field, "priority") === 0 || strcmp($field, "fiscalYear") === 0) {
        $reqResult = executeSelect("SELECT r.$field FROM requirement r WHERE r.reqId=$reqId");
        $row = mysqli_fetch_array($reqResult);
        $value = $row['' . $field . ''];
        echo '     <td class="col-2">';
        if (strcmp($field, "source") === 0) {
            echo 'Description';
            echo getFormInputElement("text", "textfieldDescription", "98%", $value, NULL, NULL, NULL, "Edit the source this requirement originated from");
            echo 'Source';
            echo getFormInputElement("text", "textfieldSource", "98%", $value, NULL, NULL, NULL, "Edit the source this requirement originated from");
        } else if (strcmp($field, "priority") === 0) {
            echo 'Priority</br>';
            echo $value;
            //getDropDownWithSelection($array, 'dropdownPriority', $value);
            //echo getFormInputElement("text", "textfieldPriority", "98%", $value);
        } else {
            echo 'Fiscal Year </br>';
            echo getDropDownMenu(getFiscalYearArray(), "fyDropdown", $value, "Edit the Fiscal Year this requirement applies to");
            //return a delete button if this requirement is not related to any subtasks
            $reqResult = executeSelect("SELECT * FROM subtask WHERE reqId=$reqId");
            if ($reqResult && $reqResult->num_rows > 0) {
                echo getHiddenField('reqID', $reqId);
                echo "<br><br><input type=\"submit\" value=\"Delete Requirement\" name=\"Delete Requirement\" title=\"Delete this requirement from the database\">";
            }
        }
        echo '     </td>';
    } else if (strcmp($field, "other") === 0) {
        $reqResult = executeSelect("SELECT * FROM requirement r WHERE r.reqId=$reqId");
        $row = mysqli_fetch_array($reqResult);
        $description = $row['description'];
        $source = $row['source'];
        $priority = $row['priority'];
        $fy = $row['fiscalYear'];

        echo '<td class="col-2">';
        echo 'Description';
        echo getFormInputElement("text", "textfieldDescription", "98%", $description, NULL, NULL, NULL, "Edit the name or description of the requirement");
        echo '<br><br>';
        echo 'Source';
        echo getFormInputElement("text", "textfieldSource", "98%", $source, NULL, NULL, NULL, "Edit the source this requirement originated from");
        echo '<br><br>';
        echo 'Priority: ';
        echo $priority;
        echo '<br><br>';
        //getDropDownWithSelection($array, 'dropdownPriority', $value);
        //echo getFormInputElement("text", "textfieldPriority", "98%", $value);        
        echo 'Fiscal Year </br>';
        echo getDropDownMenu(getFiscalYearArray(), "fyDropdown", $fy, "Edit the Fiscal Year this requirement applies to");
        //return a delete button if this requirement is not related to any subtasks
        $reqDeleteResult = executeSelect("SELECT * FROM taskreqmap WHERE reqId=$reqId");
        if (!$reqDeleteResult || $reqDeleteResult->num_rows === 0) {
            echo "<br><br><input type=\"submit\" value=\"Delete Requirement\" name=\"submit\" title=\"Delete this requirement from the database\">";
            echo getHiddenField("reqID", $reqId);
        }

        echo '     </td>';
    } else {
        echo 'Unknown parameter';
    }
}

/**
 * Queries the db and returns all of the associated parameters to the given req ID
 * @param type $reqId
 * @param type $selectedItem
 */
function getParametersFromReq($reqId, $returnDropdown = NULL, $onChange = NULL, $selectedItem = NULL) {

    //get the edrs in the edrReqMap
    $sqlSelect = "SELECT p.name, p.parameterID FROM reqparametermap map, parameter p WHERE p.parameterID=map.parameterID AND map.reqId=$reqId";
    $result = executeSelect($sqlSelect);
    //echo "size $result->num_rows <br>";
    if ($result) {

        //testing
        if (!$returnDropdown) {
            $selectedParamsArray = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($selectedParamsArray, $row['parameterID']);
            }
            $allParamsResult = getAllFieldsInTable("parameter", "name");
            //echo '<div class="container2">';
            $counter = 0;
            while ($row = mysqli_fetch_array($allParamsResult)) {
                $paramName = $row['name'];
                $paramID = $row['parameterID'];
                $isChecked = false;
                //iterate through the selected items
                for ($i = 0; $i < sizeof($selectedParamsArray); $i++) {
                    if (strcmp($selectedParamsArray[$i], $paramID) === 0) {
                        $isChecked = true;
                        break;
                    }
                }
                if ($isChecked) {
                    echo "<input name =\"checkbox$counter\" id =\"checkbox$counter\" value = \"$paramID\"type=\"checkbox\" checked/>$paramName<br />";
                } else {
                    echo "<input name =\"checkbox$counter\" id = name =\"checkbox$counter\" value = \"$paramID\"type=\"checkbox\"/>$paramName<br />";
                }
                $counter++;
            }
        }
        //old way that works
        else {
            echo '<select name="dropdownParameter" id="dropdownParameter" onchange="' . $onChange . '">';
            echo '<option value=defaultSelection>(Select)</option>';
            // echo "HUHU <br>";
            while ($row = mysqli_fetch_array($result)) {
                $paramName = $row['name'];
                $paramID = $row['parameterID'];
                if ($selectedItem !== NULL && strcmp($paramName, $selectedItem) == 0) {
                    echo '<option selected value="' . $paramID . '">';
                    echo $paramName;
                    echo'</option>';
                } else {
                    echo '<option value="' . $paramID . '">';
                    echo $paramName;
                    echo'</option>';
                }
            }
            echo '</select>';
        }
    } else {
        echo 'Error selecting req edr map ' . $sqlSelect . '<br>';
    }
}

function getParametersFromReqText($reqId, $returnDropdown = NULL, $onChange = NULL, $selectedItem = NULL) {

    $output = "";
    //get the edrs in the edrReqMap
    $sqlSelect = "SELECT p.name, p.parameterID FROM reqparametermap map, parameter p WHERE p.parameterID=map.parameterID AND map.reqId=$reqId";
    $result = executeSelect($sqlSelect);
    //echo "size $result->num_rows <br>";
    if ($result) {

        //testing
        if (!$returnDropdown) {
            $selectedParamsArray = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($selectedParamsArray, $row['parameterID']);
            }
            $allParamsResult = getAllFieldsInTable("parameter", "name");
            //echo '<div class="container2">';
            $counter = 0;
            while ($row = mysqli_fetch_array($allParamsResult)) {
                $paramName = $row['name'];
                $paramID = $row['parameterID'];
                $isChecked = false;
                //iterate through the selected items
                for ($i = 0; $i < sizeof($selectedParamsArray); $i++) {
                    if (strcmp($selectedParamsArray[$i], $paramID) === 0) {
                        $isChecked = true;
                        break;
                    }
                }
                if ($isChecked) {
                    $output .= "<input name =\"checkbox$counter\" id =\"checkbox$counter\" value = \"$paramID\"type=\"checkbox\" checked/>$paramName<br />";
                } else {
                    $output .= "<input name =\"checkbox$counter\" id = name =\"checkbox$counter\" value = \"$paramID\"type=\"checkbox\"/>$paramName<br />";
                }
                $counter++;
            }
        }
        //old way that works
        else {
            $output .= '<select name="dropdownParameter" id="dropdownParameter" onchange="' . $onChange . '">';
            $output .= '<option value=defaultSelection>(Select)</option>';
            // echo "HUHU <br>";
            while ($row = mysqli_fetch_array($result)) {
                $paramName = $row['name'];
                $paramID = $row['parameterID'];
                if ($selectedItem !== NULL && strcmp($paramName, $selectedItem) == 0) {
                    $output .= '<option selected value="' . $paramID . '">';
                    $output .= $paramName;
                    $output .='</option>';
                } else {
                    $output .= '<option value="' . $paramID . '">';
                    $output .= $paramName;
                    $output .='</option>';
                }
            }
            $output .= '</select>';
        }
    } else {
        $output .= 'Error selecting req edr map ' . $sqlSelect . '<br>';
    }
    return $output;
}

function getSensorsFromSat($satID, $dropdownType = NULL) {

    //get the edrs in the edrReqMap
    $sqlSelect = "SELECT DISTINCT sensor.sensorID, sensor.name FROM sensor, satsensormap ssm 
    WHERE sensor.sensorID=ssm.sensorID 
    AND ssm.satelliteID=$satID";
    $selectedSensorResult = executeSelect($sqlSelect);
    //echo "size $result->num_rows <br>";
    if ($selectedSensorResult) {
        $selectedSensorsArray = array();
        while ($row = mysqli_fetch_array($selectedSensorResult)) {
            array_push($selectedSensorsArray, $row['sensorID']);
        }
        $allSensorsResult = getAllFieldsInTable("sensor", "name");
        //echo '<div class="container2">';
        $counter = 0;
        //echo '<div class="container">';
        while ($row = mysqli_fetch_array($allSensorsResult)) {
            $sensorName = $row['name'];
            $sensorID = $row['sensorID'];
            $isChecked = false;
            //iterate through the selected items
            for ($i = 0; $i < sizeof($selectedSensorsArray); $i++) {
                if (strcmp($selectedSensorsArray[$i], $sensorID) === 0) {
                    $isChecked = true;
                    break;
                }
            }
            if ($isChecked) {
                echo "<input name =\"checkbox$counter\" value = \"$sensorID\"type=\"checkbox\" checked/>$sensorName<br />";
            } else {
                echo "<input name =\"checkbox$counter\" value = \"$sensorID\"type=\"checkbox\"/>$sensorName<br />";
            }
            $counter++;
        }
        //echo '</div>';
    } else {
        echo 'Error selecting req edr map ' . $sqlSelect . '<br>';
    }
}

function getSensorAcronymsOnSat($satID, $listType = NULL) {

    //get the edrs in the edrReqMap
    $sqlSelect = "SELECT DISTINCT sensor.sensorID, sensor.acronym FROM sensor, satsensormap ssm 
        WHERE sensor.sensorID=ssm.sensorID 
        AND ssm.satelliteID=$satID";
    $selectedSensorResult = executeSelect($sqlSelect);
    //echo "size $result->num_rows <br>";
    if ($selectedSensorResult) {
        if ($listType && strcmp($listType, "single") === 0) {
            getGenericDropdownMenu($selectedSensorResult, 'sensorDropdown', '98%', false, NULL, 'sensorDropdown');
        } else {
            $selectedSensorsArray = array();
            while ($row = mysqli_fetch_array($selectedSensorResult)) {
                array_push($selectedSensorsArray, $row['sensorID']);
            }
            $allSensorsResult = getAllFieldsInTable("sensor", "acronym");
            //echo '<div class="container2">';
            $counter = 0;
            //echo '<div class="container">';
            while ($row = mysqli_fetch_array($allSensorsResult)) {
                $sensorName = $row['acronym'];
                $sensorID = $row['sensorID'];

                $isChecked = false;
                //iterate through the selected items
                for ($i = 0; $i < sizeof($selectedSensorsArray); $i++) {
                    if (strcmp($selectedSensorsArray[$i], $sensorID) === 0) {
                        $isChecked = true;
                        break;
                    }
                }
                if ($isChecked) {
                    echo "<input name =\"checkbox$counter\" id =\"checkbox$counter\" value = \"$sensorID\"type=\"checkbox\" checked/>$sensorName<br />";
                } else {
                    echo "<input name =\"checkbox$counter\" id =\"checkbox$counter\" value = \"$sensorID\"type=\"checkbox\"/>$sensorName<br />";
                }
                $counter++;
            }
        }
        //echo '</div>';
    } else {
        echo 'Error selecting req edr map ' . $sqlSelect . '<br>';
    }
}

function getProgrammeAgencies($programmeID, $onChange = NULL, $selectedItem = NULL) {

    //get the agencies associated with this programme
    $sqlSelect = "SELECT * FROM spaceagency sa, satelliteprogrammespaceagencymap map WHERE map.satelliteProgrammeID='$programmeID' AND map.spaceAgencyID=sa.spaceAgencyID";
    $selectedAgencies = executeSelect($sqlSelect);
    if ($selectedAgencies) {
        $selectedAgencyArray = array();
        while ($row = mysqli_fetch_array($selectedAgencies)) {
            array_push($selectedAgencyArray, $row['spaceAgencyID']);
        }
        //get all agencies
        $allAgencyResult = getAllFieldsInTable("spaceagency", "acronym");
        //echo '<div class="container2">';
        $counter = 0;
        //echo '<div class="container">';
        while ($row = mysqli_fetch_array($allAgencyResult)) {
            $acronym = $row['acronym'];
            $spaceAgencyID = $row['spaceAgencyID'];
            $isChecked = false;
            //iterate through the selected items
            for ($i = 0; $i < sizeof($selectedAgencyArray); $i++) {
                if (strcmp($selectedAgencyArray[$i], $spaceAgencyID) === 0) {
                    $isChecked = true;
                    break;
                }
            }
            if ($isChecked) {
                echo "<input name =\"checkbox$counter\" id =\"checkbox$counter\" value = \"$spaceAgencyID\"type=\"checkbox\" checked/>$acronym<br />";
            } else {
                echo "<input name =\"checkbox$counter\" id =\"checkbox$counter\" value = \"$spaceAgencyID\"type=\"checkbox\"/>$acronym<br />";
            }
            $counter++;
        }
        //echo '</div>';
    } else {
        echo 'Error selecting req edr map ' . $sqlSelect . '<br>';
    }
}

//echo $edrDescription;
//get all distinct satellites that support this edr
//change this we need 

function getSatellitesFromParameter($parameterID, $selectedItem = NULL) {
    //    $sqlSelect = "SELECT DISTINCT satellite.satelliteID, satellite.name FROM edr,satellite 
    //              WHERE '$edrDescription'=edr.description AND edr.satelliteID=satellite.satelliteID";

    $sqlSelect = "SELECT DISTINCT s.satelliteID, s.acronym 
            FROM edr e,satellite s, parameter p, satsensormap ssm
            WHERE '$parameterID'= p.parameterID AND 
            e.parameterID=p.parameterID AND 
            e.satSensorMapID = ssm.satSensorMapID AND
            ssm.satelliteID=s.satelliteID";
    //echo $sqlSelect.'<br>';
    $result = executeSelect($sqlSelect);
    if ($result) {
        // echo 'Good result';
        if ($selectedItem === NULL) {
            getGenericDropdownMenu($result, "satDropdown", NULL, false, "getSensorFromSatAndEdr(this.value)", "satDropdown");
        } else {
            //getTwoArgDropDownWithActionAndSelectionAndId($result, "satDropdown", "getSensorFromSat(this.value)", $selectedItem, "satDropdown");
            getGenericDropdownMenu($result, "satDropdown", NULL, false, "getSensorFromSatAndEdr(this.value)", "satDropdown", $selectedItem);
        }
    } else {
        echo 'Bad result ' . mysqli_error() . '<br>';
    }
}

function getSensorFromSatAndEdr($satID, $paramID, $selectedItem = NULL) {
    //get all sensors for the given edrID and SatID
    $sqlSelect = "SELECT DISTINCT sensor.sensorID, sensor.acronym FROM sensor,edr, satsensormap ssm 
            WHERE sensor.sensorID=ssm.sensorID AND 
            ssm.satelliteID=$satID AND
            edr.satSensorMapID=ssm.satSensorMapID AND
            edr.parameterID='$paramID'";
    //echo $sqlSelect.'<br>';
    $result = executeSelect($sqlSelect);
    if ($result) {
        // echo 'Good result';
        if ($selectedItem === NULL) {
            //getTwoArgDropDown($result, "sensorDropdown");
            getGenericDropdownMenu($result, "sensorDropdown", NULL, false);
        } else {
            //getTwoArgDropDownWithSelection($result, "sensorDropdown", $selectedItem);
            getGenericDropdownMenu($result, "sensorDropdown", NULL, false, NULL, "sensorDropdown", $selectedItem);
        }
    } else {
        echo 'Bad result ' . mysqli_error() . '<br>';
    }
}

?>
